<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_model extends CI_Model {

    private $_name = 'ci_user';
    var $validate = array(
            'change_password' => array(
                    array('field'   => 'old_password'         ,'rules'   => 'required|max_length[50]|xss_clean',),
                    array('field'   => 'new_password'         ,'rules'   => 'required|max_length[50]|matches[new_password_confirm]|xss_clean',),
                    array('field'   => 'new_password_confirm' ,'rules'   => 'required|max_length[50]|xss_clean',),
            ),
    );

    public function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->library(array('form_validation','MY_Validation','MY_FormParam'));
    }

	public function checkAdmin($data = array()){
		$query = $this->db->get_where('user',$data,1);
		if ( $query->num_rows() > 0)	{
			foreach ($query->result_array() as $rows){
                $result = $rows;
            }
            return $result['id'];
		}
		return false;
	}
	
	function exec_validation($valid_type) {
	    $array    = $this->my_formparam->getFormParamList();
	    $arrValidate = $this->validate[$valid_type];
	
	    foreach($arrValidate as &$value){
	        if($value['field'] == $array[$value['field']]['keyname']) {
	            $value['label'] = $array[$value['field']]['disp_name'];
	        }
	    }
	
	    $this->my_validation->set_rules($arrValidate);
	    $status = $this->my_validation->run();
	    
	    $data['data'] = &$this->my_validation->_field_data;
	    
	    if($valid_type == 'change_password') {
    	    $params = $this->session->userdata('user_login');
    	    $params['login_pass'] = MY_Helper::generateHashString($array['old_password']['value']);
    	    if(!$id = $this->checkAdmin($params)) {
    	        $status = false;
    	        $data['data']['old_password']['error'] = 'パスワード（旧）が正しくありません。';
    	    }
	    }

	    $data['status'] = $status;
	    return $data;
	}
}